<?php

class Db_City extends Model_Db_Core {

	protected  $city   = null;
	
	function __construct(){		
	}
	
	public function getCity($id){
	    $filter  = array('id'=> $id);
	    $select  = array();
	    
	    parent::getSQLResult(__CLASS__,$filter,$select);
	    $this->city = parent::result(1);

	    return $this->city;
	}
	
	function getCitysByRadius($latitude, $longitude, $radius = 20) {
        
        $lng_min = $longitude - $radius / abs(cos(deg2rad($latitude)) * 69);
        $lng_max = $longitude + $radius / abs(cos(deg2rad($latitude)) * 69);
        $lat_min = $latitude - ($radius / 69);
        $lat_max = $latitude + ($radius / 69);
        		
		$sql = "SELECT city.id, county.name, count(county_id) as value FROM city, county WHERE (lng BETWEEN $lng_min AND $lng_max) AND (lat BETWEEN $lat_min and $lat_max) AND county.id = county_id AND county.name = city.name group by county_id order by value DESC";	
	    
	    parent::setSqlManuel($sql);
	    $citys = parent::result();
		
		return $citys;
	}
	
}

?>